﻿using System;
using System.Web.UI.WebControls;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.Web.UI.WebControls;
using ModulesToolkit.DataAccess.Membership;
using ModulesToolkit.DataContracts;
using ModulesToolkit.DataContracts.Membership;

namespace ModulesToolkit.Membership.Views
{
    public partial class RolesList : Microsoft.Practices.CompositeWeb.Web.UI.Page, IRolesListView
    {
        #region Fields

        private ScreenRightDtoSearchCriteria _screenRightDtoSearchCriteria;

        private RolesListPresenter _presenter;
        private RolesRepository _rolesRepository;
        private RolesSearchCriteria _rolesSearchCriteria;
        private ScreensSearchCriteria _screeensSearchCriteria;
      //  private ScreenRightDtoSearchCriteria _screenRightDtoSearchCriteria;

        #endregion Fields

        #region Properties

        public int EditingId
        {
            get { return this.ViewState.Get("EditingId", 0); }
            set { this.ViewState.Set("EditingId", value); }
        }

        [CreateNew]
        public RolesListPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        public ScreenRightDtoSearchCriteria ScreenRightSearchCriteria
        {
            get
            {
                if (this._screenRightDtoSearchCriteria == null)
                {
                    this._screenRightDtoSearchCriteria = new ScreenRightDtoSearchCriteria();
                }
              //  _screenRightDtoSearchCriteria.ScreenName = this.uxName.Text;
                return _screenRightDtoSearchCriteria;
            }
        }

        public Role Role
        {
            get
            {
                return new Role()
                {
                    Name = this.uxName.Text,
                    Id = this.EditingId
                };
            }
            set
            {
                this.uxName.Text = value.Name;
            }
        }

        public ScreensSearchCriteria ScreensSearchCriteria
        {
            get
            {
                if (_screeensSearchCriteria == null)
                {
                    _screeensSearchCriteria = new ScreensSearchCriteria();
                }

                return _screeensSearchCriteria;
            }
        }

        public RolesSearchCriteria SearchCriteria
        {
            get
            {
                if (_rolesSearchCriteria == null)
                {
                    _rolesSearchCriteria = new RolesSearchCriteria();
                }
                _rolesSearchCriteria.Name = this.uxName.Text;
                _rolesSearchCriteria.PageNumber = this.uxRolesList.PageIndex + 1;
                _rolesSearchCriteria.PageSize = this.uxRolesList.PageSize;
                return _rolesSearchCriteria;
            }
        }
     

        #endregion Properties

        #region Methods

        public void FillRightsList(EntitySearchResult<ScreenRightsDto> rightsList)
        {
            //this.uxRightsListSource.DataSource = rightsList;
        }

        public void FillRolesList(EntitySearchResult<Role> entitySearchResult)
        {
            this.uxRolesListSource.DataSource = entitySearchResult;
        }

        public void FillScreensAccordion(EntitySearchResult<Screens> entityscreens)
        {
            this.uxScreensAccordion.DataSource = entityscreens.Items;
            this.uxScreensAccordion.DataBind();
        }
        public void FillScreenRights(EntitySearchResult<ScreenRightsDto> screenRight)
        {
            //this.uxScreensAccordion.DataSourceID.Equals("uxSreensRights_DataSource");
            //this.uxScreensAccordion.DataSource = screenRight.Items;
            //this.uxScreensAccordion.DataBind();
            this.uxScreensAccordion.DataSource = screenRight.Items;
           // this.uxScreensAccordion.DataBind();
        }
        public void ShowDetailsPopup()
        {
            this.uxRolesDetails.Show();
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        protected void uxAdd_Click(object sender, EventArgs e)
        {
            this.Presenter.OnAddCommandClik();
        }

        protected void uxNameFieldDublicationValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            // args.IsValid = this.Presenter.
        }

        //protected void uxRightsListSource_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
        //{
        //    this.ScreenRightSearchCriteria.SortField = e.GetSortExpression();
        //    this.ScreenRightSearchCriteria.SortDirection = e.GetSortDirection();
        //    this.Presenter.OnLoadScreenRights();
        //}

        //protected void uxRightsList_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        e.Row.FindControl<CheckBox>("uxIsRightCheckedField").Checked =
        //            this.Presenter.IsInRights(((ScreenRightsDto)e.Row.DataItem).RightId);
        //    }
        //}

        protected void uxRolesList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Deleting")
            {
                this.Presenter.OnDeleteCommand(e.CommandArgument.ConvertTo(0));
            }
        }

        protected void uxRolesList_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
        {
            this.SearchCriteria.SortField = e.GetSortExpression();
            this.SearchCriteria.SortDirection = e.GetSortDirection();
            this.Presenter.OnLoadRolesList();
        }

        //protected void uxScreensListSelect(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
        //{
        //    e.AccordionItem.DataBind();
        //    this.Presenter.OnLoadScreens();
        //    this.Presenter.OnLoadScreens();
        //}

        //protected void uxScreensRights_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
        //{
        //    this.Presenter.OnLoadScreens();
        //    this.ScreenRightSearchCriteria.SortField = e.GetSortExpression();
        //    this.ScreenRightSearchCriteria.SortDirection = e.GetSortDirection();
        //    this.Presenter.OnLoadScreenRights();
        //}
        protected void uxScreenRightsBinding(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
        {
            //this.Presenter.OnLoadScreenRights();
            e.AccordionItem.FindControl("uxScreensRights");
            e.AccordionItem.ItemType.Equals(AjaxControlToolkit.AccordionItemType.Content);
            this.Presenter.OnLoadScreenRights();
            e.AccordionItem.DataBind();
        }
        protected void uxSearch_Click(object sender, EventArgs e)
        {
            this.Presenter.OnSearchClick();
        }

        #endregion Methods
    }
}